package com.example.im.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.im.R
import com.xlkj.ehobe.storage.ui.adapter.holder.RecyclerViewHolder
import kotlinx.android.synthetic.main.recycler_item_chat_emoji.view.*

class ChatEmojiAdapter(private val mContext: Context, private val emojis: MutableList<Int>) :
    RecyclerView.Adapter<RecyclerViewHolder>() {

    private var onItemClickListener: ((View, Int) -> Unit)? = null

    fun setOnItemClickListener(onItemClickListener: ((View, Int) -> Unit)) {
        this.onItemClickListener = onItemClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        val inflate = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_chat_emoji, parent, false)
        return RecyclerViewHolder(inflate)
    }

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        val view = holder.itemView
        view.text_emoji.text = String(Character.toChars(emojis[position]))
        view.setOnClickListener { v ->
            onItemClickListener!!.invoke(v, position)
        }
        view.tag = position
    }

    override fun getItemCount(): Int {
        return emojis.size
    }

    interface OnItemClickListener {
        fun onItemClick(view: View, position: Int)
    }
}
